package com.ciwong.epaper.modules.reciteWords;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by keepon on 2018/9/7.
 */
public class ScheduleWordCountManager extends TimeManger {



	private static final int MIN_COUNT__ONE_DAY = 5;
	private static final int MAX_COUNT__ONE_DAY = 1000;
	Map<Integer, Integer> workMap = new HashMap<>();
	Map<Integer, Integer> dayMap  = new HashMap();

	public ScheduleWordCountManager(String str, String str2) {
		super(str, str2);
	}
	public ScheduleWordCountManager() {
		super();
	}

	/**
	 *
	 * @param planDays 计划天数
	 * @param countOneDay 每天多少个
	 */
	public void compute(int planDays, int countOneDay) {
		try {
			dayMap.put(Integer.valueOf(countOneDay), Integer.valueOf(planDays));
			if (!workMap.containsKey(Integer.valueOf(planDays))) {
				workMap.put(Integer.valueOf(planDays), Integer.valueOf(countOneDay));
			} else if (countOneDay < ((Integer) workMap.get(Integer.valueOf(planDays))).intValue()) {
				workMap.put(Integer.valueOf(planDays), Integer.valueOf(countOneDay));
			}
		}catch (Exception e){

		}

	}
	@Override
	public void initData(int wordCount) {
		if (wordCount <= MIN_COUNT__ONE_DAY) {
			workMap.put(Integer.valueOf(1), Integer.valueOf(MIN_COUNT__ONE_DAY));
			dayMap.put(Integer.valueOf(MIN_COUNT__ONE_DAY), Integer.valueOf(1));
		} else {
			int maxCountOneDay = Math.min(wordCount ,MAX_COUNT__ONE_DAY);
			//每天多少个
			int countOneDay = MIN_COUNT__ONE_DAY;
			while (countOneDay <= maxCountOneDay) {
				int addCount;
				//计划天数
				int planDays = wordCount / countOneDay;
				//整除的话不需要加一天,否则加一天
				if (wordCount % countOneDay == 0) {
					addCount = 0;
				} else {
					addCount = 1;
				}
				compute(addCount + planDays, countOneDay);
//				Log.e("TAG", "initData: addCount=="+addCount+" planDays=="+planDays+" addCount + planDays=="+(addCount + planDays)+" minCountOneDay=="+countOneDay );
				countOneDay = progressive(countOneDay) + countOneDay;
			}
			if (!dayMap.containsKey(Integer.valueOf(maxCountOneDay))) {
				workMap.put(Integer.valueOf(1), Integer.valueOf(maxCountOneDay));
				dayMap.put(Integer.valueOf(maxCountOneDay), Integer.valueOf(1));
			}
		}
		mScheduleWordCountList.addAll(dayMap.keySet());
		mScheduleDayCountList.addAll(workMap.keySet());
		Collections.sort(mScheduleWordCountList);
		Collections.sort(mScheduleDayCountList);
	}

	private int progressive(int i) {
		if (i < 50) {
			return 5;
		}
		if (i < 100) {
			return 10;
		}
		if (i < 400) {
			return 25;
		}
		if (i >= MAX_COUNT__ONE_DAY) {
			return 100;
		}
		return 50;
	}



	/**
	 *
	 * @param index  每天单词数选中第几个index
	 * @return 对应的天数选中第几个index
	 */
	public int getSchedulWordCount(int index) {
		try {
			return mScheduleWordCountList.indexOf(Integer.valueOf(((Integer) workMap.get(Integer.valueOf(((Integer) mScheduleDayCountList.get(index)).intValue()))).intValue()));

		}catch (Exception e){
			return  0;
		}
	}

	public int getScheduleDays(int i) {
		try {
			return mScheduleDayCountList.
					indexOf(Integer.valueOf(((Integer) dayMap.get(Integer.valueOf(((Integer) mScheduleWordCountList.get(i)).intValue()))).intValue()));
		}catch (Exception e){
			return  0;
		}

	}

}
